package main

import (
	"strconv"
	"os"
	"fmt"
)
// 定义函数类型
type visit func(int)

func main229()  {
    // 切片需要在前端写入[][]int，最大索引16，总共17点
	sli := [][]int{{0,12,28},{5,6,39},{8,6,59},{13,15,7},{13,12,38},{10,12,35},{15,3,23},{7,11,26},{9,4,65},{10,2,38},{4,7,7},
	{14,15,31},{2,12,44},{8,10,34},{13,6,29},{5,14,89},{11,16,13},{7,3,46},{10,15,19},{12,4,58},{13,16,11},{16,4,76},{2,0,12},
	{15,0,22},{16,12,13},{7,1,29},{7,14,100},{16,1,14},{9,6,74},{11,1,73},{2,11,60},{10,11,85},{2,5,49},{3,4,17},{4,9,77},{16,3,47},
	{15,6,78},{14,1,90},{10,5,95},{1,11,30},{11,0,37},{10,4,86},{0,8,57},{6,14,68},{16,8,3},{13,0,65},{2,13,6},{5,13,5},{8,11,31},
	{6,10,20},{6,2,33},{9,1,3},{14,9,58},{12,3,19},{11,2,74},{12,14,48},{16,11,100},{3,12,38},{12,13,77},{10,9,99},{15,13,98},{15,12,71},
	{1,4,28},{7,0,83},{3,5,100},{8,9,14},{15,11,57},{3,6,65},{1,3,45},{14,7,74},{2,10,39},{4,8,73},{13,5,77},{10,0,43},{12,9,92},{8,2,26},
	{1,7,7},{9,12,10},{13,11,64},{8,13,80},{6,12,74},{9,7,35},{0,15,48},{3,7,87},{16,9,42},{5,16,64},{4,5,65},{15,14,70},{12,0,13},{16,14,52},
	{3,10,80},{14,11,85},{15,2,77},{4,11,19},{2,7,49},{10,7,78},{14,6,84},{13,7,50},{11,6,75},{5,10,46},{13,8,43},{9,10,49},{7,12,64},{0,10,76},
	{5,9,77},{8,3,28},{11,9,28},{12,16,87},{12,6,24},{9,15,94},{5,7,77},{4,10,18},{7,2,11},{9,5,41}}
    len1 := len(sli)
	fmt.Println(len1)
    max :=0;
	for _,val:=range sli{
		if max < val[0]{
			max = val[0]
		}
		if max < val[1] {
            max = val[1]
		}
	}
	fmt.Printf("max: %d \n",max)
	// 如何判断这个图中存在环（包括自环)
	
    in_node := topo(17,sli)
	fmt.Println(in_node)
	for idx,val:=range in_node{
		fmt.Printf("idx: %d %v \n",idx,val)
	}

}
 
func topo(num int ,edges [][]int) []int  {
	serial :=make([]int,0)
	in_node :=make([][]int,num)
	for _, val:=range edges{
		in_node[val[1]] =append(in_node[val[1]],val[0])
	}
	for num>0{
		for idx,_:=range in_node{
			//  出现入度为0
			if len(in_node[idx])==0{
				serial=append(serial,idx)
				// 对入度列表修正
				for _,val:=range in_node{
					for idx2,val2:=range val{
						if val2 == idx{
							val =append(val[0:idx2],val[idx2:]...)
						}
					}
				}
			}
		}

		num--
	}
	return serial
}

// 输出所有节点的入度
func in_count(edges [][]int) ([][]int){
	in_node :=make([][]int,17)
	for _,val:=range edges{
		// 分类器
		in_node[val[1]] =append(in_node[val[1]],val[0])
	}
	return in_node
}

func dfs(cur int,edges [][]int, label []bool,fn visit )  {
	if !label[cur]{
		fn(cur)
		label[cur]=true
	}
    for _,val:=range edges{
		if(val[0] == cur && !label[val[1]]){
			// 向里面进发
			dfs(val[1],edges,label,fn)
		}
	}
}
// 抓住一条路径，一直走下去,cur代表节点索引
func bfs(edges [][]int, label []bool,fn visit )  {
	cur :=edges[0][0]
	fn(cur)
	label[cur] = true
	// 先把要访问的存起来
	path:=make([]int,0)
	path =append(path,cur)
	for len(path)>0 {
		cur = path[0]
		path =path[1:]
		fmt.Println(len(path))
		for _,val :=range edges{
			if val[0] == cur  {
				if !label[val[1]]{
				// 访问邻节点
				fn(val[1])
				label[val[1]] = true		
				// 候选新节点
				path = append(path,val[1])
				}
			}
		}
	}
}
func visit_fn(num int)  {
	
	file,_:= os.OpenFile("./doc/path.txt",os.O_RDWR,os.ModeAppend)
	defer file.Close()
	str :="\n"+strconv.Itoa(num)
    idx,_ :=file.Seek(0,2)
	file.WriteAt([]byte(str),idx)
}
